package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.N;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/notices")
@Tag(name = "公告管理", description = "系统公告相关操作，包括公告发布、编辑、删除、状态管理等功能")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Operation(summary = "获取启用的公告", description = "获取状态为启用的公告列表，供前台首页展示使用")
    @GetMapping("/active")
    public Result<List<Notice>>getActiveNotices(){
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Notice> eq = noticeLambdaQueryWrapper.eq(Notice::getIsActive, 1);
        List<Notice> list = noticeService.list(eq);
        return Result.success(list,"获得已启用通知成功");
    }


    @GetMapping("/latest")
    @Operation(summary = "获取最新公告", description = "获取最新发布的公告列表，用于首页推荐展示")
    public Result<List<Notice>>getLatestNotices(@Parameter(description = "限制数量",example = "5")@RequestParam (defaultValue = "5") int limit){
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Notice> last = noticeLambdaQueryWrapper.orderByDesc(Notice::getUpdateTime).last("LIMIT 5");
        List<Notice> list = noticeService.list(last);
        return Result.success(list,"获得最新公告成功");
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有公告", description = "获取所有公告列表，包括启用和禁用的，供管理后台使用")
    public Result<List<Notice>>getAllNotice(){
        List<Notice> list = noticeService.list();
        return Result.success(list,"获得所有公告成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取公告", description = "根据公告ID获取单个公告的详细信息")
    public Result<Notice>getNoticeById(@Parameter(description = "公告id")@PathVariable Long id){
        Notice byId = noticeService.getById(id);
        return Result.success(byId,"根据id获取公告成功");
    }

    @PostMapping("/add")
    @Operation(summary = "发布新公告", description = "创建并发布新的系统公告")
    public Result addNotice(@RequestBody Notice notice){
        boolean save = noticeService.save(notice);
        if(save){
            return Result.success("添加公告成功");
        }else{
            return Result.success("添加公告失败");
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新公告信息", description = "修改公告的内容、标题、类型等信息")
    public Result updateNoticeById(@RequestBody Notice notice){
        boolean b = noticeService.updateById(notice);
        if(b){
            return Result.success("更新公告成功");
        }else {
            return Result.error("更新公告失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除公告", description = "根据ID删除指定的公告")
    public Result deleteById(@PathVariable Long id){
        boolean b = noticeService.removeById(id);
        if(b){
            return Result.success("删除公告成功");
        }else {
            return Result.error("删除公告失败");
        }
    }

    @PutMapping("/toggle/{id}")
    @Operation(summary = "切换公告状态", description = "启用或禁用指定的公告，禁用后不会在前台显示")
    public Result toggleNoticeStatus(@PathVariable Long id , @RequestParam Boolean isActive){
        LambdaUpdateWrapper<Notice> noticeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<Notice> eq = noticeLambdaUpdateWrapper.set(Notice::getIsActive, isActive).eq(Notice::getId, id);
        boolean update = noticeService.update(eq);
        if(update){
            return Result.success("切换公告状态成功");
        }else {
            return Result.error("切换公告状态失败");
        }
    }
}
